package com.slz3.business.task;

import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.redis.Jedis;
import com.slz3.business.frame.redis.RK;
import com.slz3.business.frame.util.LocalDT;
import com.slz3.business.frame.util.Md5Utils;
import com.slz3.business.module.memberManagement.memMember.domain.MemMember;
import com.slz3.business.module.memberManagement.memMember.enums.MemMemberSexFlag;
import com.slz3.business.module.memberManagement.memMember.enums.MemMemberSignInFlag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;

/**
 * @FileName: TaskTimer
 * @Description: 定时器任务
 */

@Slf4j
@Component
public class TaskTimer {

    /**
     *  用户同步
     * @Description：每天晚上12点20分执行
     */
    @Scheduled(cron = "0 20 0 1/1 * ?")
    private void checkSync(){
        log.info("定时同步用户开始");
        // 循环优惠券
        String key =  RK.USER_INFO.concat("*");
        Set<String> keys = Jedis.db0.kes(key);
        for (String userKey: keys) {
            // 查询用户
            Map map = Jedis.db0.getMap(userKey);
            Optional<MemMember> vo = Services.memMember.findById(map.get("memberId").toString());
            MemMember memMember = new MemMember();
            if(!vo.isPresent()){
                memMember.setIncrementId(Long.getLong(map.get("id").toString()));
                memMember.setUuid(UUID.fromString(map.get("memberId").toString()));
                memMember.setSerPhone(map.get("phone").toString());
                memMember.setSerPassWord(Md5Utils.encode(map.get("passWord").toString()));
                memMember.setSerUserName(map.get("userName").toString());
                memMember.setSerPicture(map.get("picture").toString());
                memMember.setSerGoldCoin(Integer.parseInt(map.get("goldCoin").toString()));
                memMember.setSerScore(Integer.parseInt(map.get("score").toString()));
                memMember.setSerSex("1".equals(map.get("sex").toString())? MemMemberSexFlag.NAN:MemMemberSexFlag.NV);
                memMember.setSerSignIn("1".equals(map.get("signIn").toString())? MemMemberSignInFlag.YIQIANDAO:MemMemberSignInFlag.WEIQIANDAO);
                LocalDateTime createTime = LocalDateTime.now();
                if(!StringUtils.isEmpty(map.get("createTime"))){
                    createTime = LocalDT.parseStringToDateTime(map.get("createTime").toString(),"yyyy-MM-dd HH:mm:ss");
                }
                memMember.setCreateTime(createTime);
                LocalDateTime updateTime = LocalDateTime.now();
                if(!StringUtils.isEmpty(map.get("updateTime"))){
                    updateTime = LocalDT.parseStringToDateTime(map.get("updateTime").toString(),"yyyy-MM-dd HH:mm:ss");
                }
                memMember.setLastModifyTime(updateTime);
                memMember.setSerAccountNo(map.get("accountNo").toString());
                Services.memMember.save(memMember);
            }else{
                memMember = vo.get();
                memMember.setUuid(UUID.fromString(map.get("memberId").toString()));
                memMember.setSerPhone(map.get("phone").toString());
                memMember.setSerPassWord(Md5Utils.encode(map.get("passWord").toString()));
                memMember.setSerUserName(map.get("userName").toString());
                memMember.setSerPicture(map.get("picture").toString());
                memMember.setSerGoldCoin(Integer.parseInt(map.get("goldCoin").toString()));
                memMember.setSerScore(Integer.parseInt(map.get("score").toString()));
                memMember.setSerSex("1".equals(map.get("sex").toString())? MemMemberSexFlag.NAN:MemMemberSexFlag.NV);
                memMember.setSerSignIn("1".equals(map.get("signIn").toString())?MemMemberSignInFlag.YIQIANDAO:MemMemberSignInFlag.WEIQIANDAO);
                memMember.setCreateTime(LocalDT.parseStringToDateTime(map.get("createTime").toString(),"yyyy-MM-dd HH:mm:ss"));
                memMember.setLastModifyTime(LocalDT.parseStringToDateTime(map.get("updateTime").toString(),"yyyy-MM-dd HH:mm:ss"));
                memMember.setSerAccountNo(map.get("accountNo").toString());
                Services.memMember.modify(memMember);
            }
        }
        log.info("定时同步用户结束");
    }
}